#!/usr/bin/env python
from Engine.Physics_object import Physics_object
from Engine.Const import *
from pygame import *
from Foot import Foot
from Hand import Hand
from Weapon.Guns import *
from math import pi

class Player(Physics_object):
    def __init__(self, level, il, x, y):
        self.init(level, x, y, il.player_head, (12, 12, 40, 48))
        self.dir = 0
        #feet related
        self.feet = []
        self.feet.append(Foot(8, 48, il.player_feet[0], 1))
        self.feet.append(Foot(8, 44, il.player_feet[1], 2))
        
        #guns related
        self.gun = []
        self.gun.append(Magnum(il))
        
        #hands related
        self.hands = []          
        self.hands.append(Hand(il.player_hands[0], 1, 22, 42))
        self.hands.append(Hand(il.player_hands[1], 2, 22, 36))
        
        #key related
        self.key_jump = 0
        self.key_right = 0
        self.key_left = 0
        self.key_shoot = 0
        self.key_run = 0
        self.key_grenade = 0
        self.key_w0 = 0
        self.key_w1 = 0
        self.key_w2 = 0
        self.key_w3 = 0 
        
    def update_keys(self, events):
        for e in events:
            if e.type == KEYDOWN:
                if e.key == KEYRIGHT:
                    self.key_right = 1
                elif e.key == KEYLEFT:
                    self.key_left = 1
                elif e.key == KEYJUMP:
                    self.key_jump = 1
                elif e.key == KEYSHOOT:
                    self.key_shoot = 1
                elif e.key == KEYRUN:
                    self.key_run = 1
                elif e.key == KEYGRENADE:
                    self.key_grenade = 1
                elif e.key == KEYWEAPON0:
                    self.key_w0 = 1
                elif e.key == KEYWEAPON1:
                    self.key_w1 = 1
                elif e.key == KEYWEAPON2:
                    self.key_w2 = 1
                elif e.key == KEYWEAPON3:
                    self.key_w3 = 1
            elif e.type == KEYUP:
                if e.key == KEYRIGHT:
                    self.key_right = 0
                elif e.key == KEYLEFT:
                    self.key_left = 0
                elif e.key == KEYJUMP:
                    self.key_jump = 0
                elif e.key == KEYSHOOT:
                    self.key_shoot = 0
                elif e.key == KEYRUN:
                    self.key_run = 0
                elif e.key == KEYGRENADE:
                    self.key_grenade = 0
                elif e.key == KEYWEAPON0:
                    self.key_w0 = 0
                elif e.key == KEYWEAPON1:
                    self.key_w1 = 0
                elif e.key == KEYWEAPON2:
                    self.key_w2 = 0
                elif e.key == KEYWEAPON3:
                    self.key_w3 = 0
        
    def override_update(self, level, events):
        ##foot controle
        if self.vspeed > 0:
            for f in self.feet:
                f.fall(self.vspeed)
        elif self.vspeed < 0:
            for f in self.feet:
                f.jump(self.vspeed)
        elif self.vspeed == 0 and not self.collbot:
            for f in self.feet:
                f.fall(self.vspeed)
        elif self.collbot and (self.perform_friction or self.hspeed == 0):
            for f in self.feet:
                f.stopWalking()
        elif self.vspeed == 0 and self.collbot and not self.perform_friction:
            for f in self.feet:
                f.walk(self.hspeed)
        
        ##hand controle
        if self.vspeed > 0:
            for h in self.hands:
                h.fall()
        elif self.vspeed == 0 and self.collbot and not self.perform_friction:
            for h in self.hands:
                h.walk(self.hspeed)
        else:
            for h in self.hands:
                h.stopWalking()
        
        if self.key_w0:
            if self.hands[0].gun != None:
                self.hands[0].switchGun(None)
            self.key_w0 = 0
        elif self.key_w1:
            if self.hands[0].gun != self.gun[0]:
                self.hands[0].switchGun(self.gun[0])
            self.key_w1 = 0
        
        self.doShootCheck(level)
        
        self.update_keys(events)
        ##walk left, walk right
        self.perform_friction = 1
        if self.key_left and not self.key_right:
            self.change_dir(180)
            if self.key_run:
                if self.hspeed > -10:
                    if self.collbot:
                        self.hspeed -= 1
                    else:
                        self.hspeed -= 0.2
            else:
                if self.hspeed > -5:
                    if self.collbot:
                        self.hspeed -= 1
                    else:
                        self.hspeed -= 0.2
            self.perform_friction = 0
        elif self.key_right and not self.key_left:
            self.change_dir(0)
            if self.key_run:
                if self.hspeed < 10:
                    if self.collbot:
                        self.hspeed += 1
                    else:
                        self.hspeed += 0.2
            else:
                if self.hspeed < 5:
                    if self.collbot:
                        self.hspeed += 1
                    else:
                        self.hspeed += 0.2
            self.perform_friction = 0

        ##JUMP
        if self.collbot:
            if self.key_jump:        
                self.vspeed -= float(17)
        
    def doShootCheck(self, level):
        if self.hands[0].switching_gun:
            return       
        if self.key_shoot:
            if self.hands[0].gun == None:
                self.hands[0].switchGun(self.gun[0])
                return
        if self.hands[0].gun == None:
            return
        sps = self.hands[0].gun.sps
        if self.key_shoot:
            if sps <= 0:
                self.key_shoot = 0
                if self.dir == 0:
                    self.hands[0].shoot(level, self.x, self.y, 20, self.dir * pi/180)
                else:
                    self.hands[0].shoot(level, self.x - 96, self.y, 20, self.dir * pi/180)
                return
        if sps > 0:        
            self.shoot_timer += 1
            if self.shoot_timer >= FPS / sps:
                if self.key_shoot:
                    self.shoot_timer = 0
                    if self.dir == 0:
                        self.hands[0].shoot(level, self.x, self.y, 20, self.dir * pi/180)
                    else:
                        self.hands[0].shoot(level, self.x - 96, self.y, 20, self.dir * pi/180)
    def change_dir(self, dir):
        if self.dir != dir:
            self.dir = dir
            self.image = transform.flip(self.image, True, 0)
            for f in self.feet:
                f.set_direction(dir)
            for h in self.hands:
                h.set_direction(dir)
                
    def override_back_draw(self, camera):
        self.hands[1].draw(camera, self.x, self.y)
        self.feet[1].draw(camera, self)
            
    def override_front_draw(self, camera):
        self.feet[0].draw(camera, self)
        self.hands[0].draw(camera, self.x, self.y)
        
    def get_new_instance(self, level, il):
        return Player(level, il, 0, 0)
    def bullet_hit(self, hit_pos):
        for i in range(0, 10):
            self.level.create_blood(hit_pos[0], hit_pos[1])
    def event_death(self):
        self.level.set_timer_resetplayer()
        for i in range(0, 100):
            self.level.create_blood(self.rect[0] + self.rect[2]/2, self.rect[1] + self.rect[3]/2)
        #create death player